var mids = detail.user_id;
var oids = detail.id;

var vm = new Vue({
    el:'#content',
    data:{
        gids:'',
        detail:detail,
        address:{},
        alreadypinjia:false,
        iftanchuang:false,
        ifpinlun:false,
        ifdizhi:false,
        uploadimg:false,
        uploadText:'',
        content:'',
        address_name:'',
        address_tel:'',
        address_address:'',
        formdata:{
            name: '',
            tel: '',
            province: '',
            city: '',
            area: '',
            status:0,
            address: ''
        }
    },
    methods: {
        uploadPinjia(){     //提交评价
            console.log(this.content,this.gids,mids,oids)
            var obj = new FormData
            obj.append('content',this.content)
            obj.append('pic', $('#infile')[0].files[0])
            obj.append('gid', this.gids)
            obj.append('mid', mids)
            obj.append('oid', oids)

            var options = {
                url: 'user.php?act=comments',
                data: obj,
                method: 'post',
                headers: {
                  'Content-Type': 'multipart/form-data'
                }
            }

            axios(options).then((res) => {
                if(res.data == 1){
                    alert('评论成功')
                    this.close('pinlun')
                    window.location.href = 'user.php?act=order_detail&id=' + detail.id
                }else{
                    alert('评论失败')
                }
                console.log(res)
            }) // 发送请求
        },
        getStatusdata(){    //加载地址
            this.address = detail.address
        },
        pinjiahandle(id){   //点击评价按钮
            if(!alreadypinjia){
                console.log(id)
                this.gids = id
                this.iftanchuang = true
                this.ifpinlun = true
            }else{
                console.log('已经评价')
            }
        },
        locationhandle(){   //点击地址按钮
            this.iftanchuang = true
            this.ifdizhi = true
            $('.tanchuang').css({'opacity':1,'z-index':'200'})
            console.log($('.address_address')[0].innerHTML)
            this.address_name = $('.address_name')[0].innerHTML
            this.address_tel = $('.address_tel')[0].innerHTML
            this.address_address = $('.address_address')[0].innerHTML
        },
        ok(){       //完成地址修改
            this.address.name = this.address_name
            this.address.tel = this.address_tel
            var maxlen = $('#sel_city')[0].innerHTML
            var minlen = maxlen.split("-")
            this.formdata.province = minlen[0] + '省'
            this.formdata.city = minlen[1] + '市'
            this.formdata.area = minlen[2] + " "
            this.address.address = this.formdata.province + this.formdata.city + this.formdata.area + $('#pinlun_inputs')[0].value + ""
            this.close('location')
        },
        // gopay(order_id){
        //     console.log(this.address,detail.goods)
        //     const data = new URLSearchParams()
        //     const goods = detail.goods.map(e =>({
        //         iscomment:0,
        //         id: e.id,
        //         price: e.price,
        //         num: e.shop_num,
        //         name: e.title
        //     }))
        //     console.log(goods)
        //     const address = {
        //         name: this.address.name,
        //         tel: this.address.tel,
        //         address: this.address.address
        //     }
        //     console.log(address)
        //     console.log(detail.price)
        //     data.append('goods', JSON.stringify(goods))
        //     data.append('address', JSON.stringify(address))
        //     data.append('total', detail.price)
        //     data.append('remarks', 'I am remarks')
        //     axios.post('user.php?act=add2order&ajax=true', data).then(r => {
        //         if (r.data.code == 1) {
        //             alert('操作成功')
        //             // this.getAllData()
        //             window.location.href = 'user.php?act=order_success&order_id='+r.data.order_id
        //         } else {
        //             alert('操作失败')
        //             // this.$message.error('兄弟，醒醒，没钱还想下单？')
        //         }
        //     })
        // },
        saveLocation(){  //保存地址
            var maxlen = $('#sel_city')[0].innerHTML
            var minlen = maxlen.split("-")
            this.formdata.province = minlen[0] + '省'
            this.formdata.city = minlen[1] + '市'
            this.formdata.area = minlen[2] + " "
            this.formdata.name = this.address_name
            this.formdata.tel = this.address_tel
            var data = new URLSearchParams()
            for (let key in this.formdata) {
                // console.log(key)
                console.log(this.formdata[key])
                data.append(key, this.formdata[key])
            }
            data.append('address_id', '')
            axios.post('user.php?act=update_or_insert_address&ajax=true',data).then(e =>{
                if(e.data.code == 1){
                    alert('请求成功')
                    this.ok()
                    // window.location.href = '/m/user.php?act=account'
                }
            })
        },
        removeDindan(){  //取消订单
            if(parseInt(detail.order_status) != 1){
                alert('当前订单不可取消')
                return false
            }else{
                axios.get('user.php?act=cancelorder&ajax=true&order_id=' + this.detail.order_id).then(r => {
                    if (r.data == 1) {
                        this.detail.order_status = -1
                        alert('操作成功')
                        window.location.href = 'user.php?act=order_detail&id=' + detail.id
                    } else {
                        alert('操作失败')
                    }
                }).catch(e =>{
                        alert('操作失败')
                })
            }
        },
        addressA(){
        },
        close(data){        //点击关闭按钮
            if(data == 'pinlun'){
                console.log('pinlun')
                this.iftanchuang = false
                this.ifpinlun = false
                this.uploadimg = false
            }else if(data == 'location'){
                console.log('location')
                $('.tanchuang').css({'opacity':0,'z-index':'-1'})
            }
        },
        uploadImg(){       //上传图片
            $('#infile').click()
            this.uploadimg = true
            $('#infile').change(function(){
                console.log(this.uploadimg)
                $('.nowimg').attr({'src':getobjs($(this)[0].files[0])})
            })
        }
    },
    created() {
        this.getStatusdata()
        this.iftanchuang = true
        this.ifdizhi = true
        $('.tanchuang').css({'opacity':0,'z-index':'-1'})
    },
    filters:{
        timeFilter(data){
            var times = new Date(data*1000)
            return times.toLocaleString()
        },
        statusFilter(data){
            switch(parseInt(data)){
                case -1:
                    return '已取消'
                case 1:
                    return '待支付'
                case 2:
                    return '待发货'
                case 3:
                    return '待收货'
                case 4:
                    return '已完成'
                case -2:
                    return '已删除'
            }
        },
        pinjia(data){
            console.log(data)
            if(data == 1){
                this.alreadypinjia = true
                return '已评价'
            }else{
                this.alreadypinjia = false
                return '立即评价'
            }
        },
        remove(data){
            if(parseInt(data) == -1){
                return '已取消'
            }else{
                return '取消订单'
            }
        }
    }
})

function getobjs(file) {
    var url = null;
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)  
      url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome  
      url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

